/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */
package nl.dualit.clazzified.core.model.relation;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nl.dualit.clazzified.core.model.clazz.Clazz;
import nl.dualit.clazzified.store.clazz.RelationFields;


abstract class AbstractRelation implements IRelation {
	
	private Long id;
	private String name;
	private String description;
	private RelationTypes type;
	private Clazz clazz;
	private List<Clazz> peers;
	
	public RelationTypes getType() {
		return type;
	}

	protected void setType( RelationTypes type ){
		this.type = type;
	}
//	public void setTypeName( RelationTypes type ) {
//		this.type = type;
//	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName(){
		return name;
	}
	
	public String getDescription(){
		return description;
	}
	
	public Clazz getClazz(){
		return clazz;
	}
	
	@Override
	public List<Clazz> getPeers(){
		return Collections.unmodifiableList( peers );
	}
	
	@Override
	public void addPeer( Clazz clazz ){
		this.peers.add( clazz );
	}
	
	@Override
	public void removePeer( Clazz clazz ){
		
		if( clazz == null )
			throw new NullPointerException( "Clazz is null" );
		if( !peers.contains( clazz ) )
			throw new IllegalArgumentException( "Clazz not a peer" ); 
		
		peers.remove( clazz );
	}
	
	
	void setClazz( Clazz clazz){
		this.clazz = clazz;
	}
	
	@Override
	public void setName( String name ){
		
		this.name = name;
	}
	
	@Override
	public void setDescription( String description ){
		
		this.description = description;
	}
	
	void setPeers( List<Clazz> peers ){
		
		this.peers = peers;
	}
	
	@Override
	public Map<RelationFields,String> getDataMap(){
		
		Map<RelationFields,String> map = new HashMap<RelationFields,String>();
		if( id != null )
			map.put( RelationFields.RELATION_ID, id.toString() );
		map.put( RelationFields.RELATION_NAME, name );
		map.put(RelationFields.RELATION_DESCRIPTION, description );
		map.put( RelationFields.TYPE_NAME, type.typeName() );
		
		return map;
	}
}
